package com.car.loan.project.ve.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.car.loan.common.enums.OperatorStatus;
import com.car.loan.common.utils.BeanUtils;
import com.car.loan.common.utils.SecurityUtils;
import com.car.loan.common.utils.StringUtils;
import com.car.loan.framework.aspectj.lang.annotation.DataScope;
import com.car.loan.framework.web.domain.AjaxResult;
import com.car.loan.project.cd.domain.CdCreditAlbum;
import com.car.loan.project.cd.domain.vo.CreditAlbumVo;
import com.car.loan.project.cd.util.enums.CreditAlbumType;
import com.car.loan.project.cd.util.enums.CreditStatus;
import com.car.loan.project.system.service.ISysSequenceService;
import com.car.loan.project.ve.domain.VeVehicleAlbum;
import com.car.loan.project.ve.domain.VeVehicleEvaluation;
import com.car.loan.project.ve.domain.bo.VehicleEvaluationApplyAddBo;
import com.car.loan.project.ve.domain.bo.VehicleEvaluationApplyUpdateBo;
import com.car.loan.project.ve.domain.bo.VehicleEvaluationInputBo;
import com.car.loan.project.ve.domain.select.VehicleEvaluationBaseSelect;
import com.car.loan.project.ve.domain.vo.VehicleEvaluationBaseVo;
import com.car.loan.project.ve.mapper.VeVehicleAlbumMapper;
import com.car.loan.project.ve.mapper.VeVehicleEvaluationMapper;
import com.car.loan.project.ve.service.IVeVehicleEvaluationService;
import com.car.loan.project.ve.util.enums.VehicleStatus;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

/**
 * 车辆评估Service业务层处理
 *
 * @author ruoyi
 * @date 2019-10-29
 */
@Service
public class VeVehicleEvaluationServiceImpl implements IVeVehicleEvaluationService {

    @Autowired
    private VeVehicleEvaluationMapper veVehicleEvaluationMapper;

    @Autowired
    private VeVehicleAlbumMapper veVehicleAlbumMapper;

    @Autowired
    private ISysSequenceService sequenceAssessServiceImpl;

    /**
     * 查询车辆评估
     *
     * @param id 车辆评估ID
     * @return 车辆评估
     */
    @Override
    public VeVehicleEvaluation selectVeVehicleEvaluationById(Long id) {
        return veVehicleEvaluationMapper.selectById(id);
    }

    /**
     * 查询车辆评估列表
     *
     * @param veVehicleEvaluation 车辆评估
     * @return 车辆评估
     */
    @Override
    public List<VeVehicleEvaluation> selectVeVehicleEvaluationList(
        VeVehicleEvaluation veVehicleEvaluation) {
        QueryWrapper<VeVehicleEvaluation> queryWrapper = new QueryWrapper<>(veVehicleEvaluation);
        return veVehicleEvaluationMapper.selectList(queryWrapper);
    }

    @Override
    public List<VeVehicleEvaluation> selectVeVehicleEvaluationQueryList(
        QueryWrapper<VeVehicleEvaluation> queryWrapper) {
        return veVehicleEvaluationMapper.selectList(queryWrapper);
    }

    @Override
    public IPage<VeVehicleEvaluation> selectPage(IPage<VeVehicleEvaluation> page,
        QueryWrapper<VeVehicleEvaluation> queryWrapper) {
        return veVehicleEvaluationMapper.selectPage(page, queryWrapper);
    }

    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public Page<VehicleEvaluationBaseVo> findVehicleEvaluationBasePage(
        VehicleEvaluationBaseSelect vehicleEvaluationBaseSelect) {
        Page<VehicleEvaluationBaseVo> page = new Page<>(vehicleEvaluationBaseSelect.getPageNum(),
            vehicleEvaluationBaseSelect.pageSize);
        return veVehicleEvaluationMapper
            .findVehicleEvaluationBasePage(page, vehicleEvaluationBaseSelect);
    }

    @Override
    public VeVehicleEvaluation save(VehicleEvaluationApplyAddBo vehicleEvaluationApplyAddBo) {
        VeVehicleEvaluation veVehicleEvaluation = new VeVehicleEvaluation();
        BeanUtils.copyBeanProp(veVehicleEvaluation, vehicleEvaluationApplyAddBo);
        veVehicleEvaluation.setNo(sequenceAssessServiceImpl.next());

        Integer operatorStatus = vehicleEvaluationApplyAddBo.getOperatorStatus();
        veVehicleEvaluation.setStatus(VehicleStatus.INIT.status);
        if (OperatorStatus.SAVE.status == operatorStatus) {
            veVehicleEvaluation.setStatus(VehicleStatus.INIT.status);
        } else if (OperatorStatus.SUBMIT.status == operatorStatus) {
            veVehicleEvaluation.setStatus(VehicleStatus.SUBMIT.status);
            veVehicleEvaluation.setSubmitAdminId(SecurityUtils.getLoginUserId());
            veVehicleEvaluation.setSubmitTime(LocalDateTime.now());
        }
        Long loginUserId = SecurityUtils.getLoginUserId();
        veVehicleEvaluation.setCreatorId(loginUserId);
        veVehicleEvaluation.setCreateTime(LocalDateTime.now());
        veVehicleEvaluationMapper.insert(veVehicleEvaluation);

        //添加图片信息
        List<CreditAlbumVo> images = vehicleEvaluationApplyAddBo.getImages();
        if (StringUtils.isNotEmpty(images)) {
            for (CreditAlbumVo image : images) {
                VeVehicleAlbum veVehicleAlbum = new VeVehicleAlbum();
                veVehicleAlbum.setVehicleId(Long.valueOf(veVehicleEvaluation.getId()));
                veVehicleAlbum.setType(CreditAlbumType.IMAGE.type);
                veVehicleAlbum.setUrl(image.getUrl());
                veVehicleAlbumMapper.insert(veVehicleAlbum);
            }
        }

        return veVehicleEvaluation;
    }

    @Override
    public AjaxResult update(VehicleEvaluationApplyUpdateBo vehicleEvaluationApplyUpdateBo) {
        VeVehicleEvaluation veVehicleEvaluation = veVehicleEvaluationMapper
            .selectById(vehicleEvaluationApplyUpdateBo.getId());
        if (null == veVehicleEvaluation) {
            return AjaxResult.error("无该申请记录");
        }
        if (!(VehicleStatus.INIT.status == veVehicleEvaluation.getStatus()
            || VehicleStatus.REVOKE.status == veVehicleEvaluation.getStatus())) {
            return AjaxResult.error("已提交无法编辑");
        }
        BeanUtils.copyBeanProp(veVehicleEvaluation, vehicleEvaluationApplyUpdateBo);
        Integer operatorStatus = vehicleEvaluationApplyUpdateBo.getOperatorStatus();
        veVehicleEvaluation.setStatus(VehicleStatus.INIT.status);
        if (OperatorStatus.SAVE.status == operatorStatus) {
            veVehicleEvaluation.setStatus(VehicleStatus.INIT.status);
        } else if (OperatorStatus.SUBMIT.status == operatorStatus) {
            veVehicleEvaluation.setStatus(VehicleStatus.SUBMIT.status);
            veVehicleEvaluation.setSubmitAdminId(SecurityUtils.getLoginUserId());
            veVehicleEvaluation.setSubmitTime(LocalDateTime.now());
        }
        veVehicleEvaluationMapper.updateById(veVehicleEvaluation);
        //添加图片信息
        List<CreditAlbumVo> images = vehicleEvaluationApplyUpdateBo.getImages();
        if (StringUtils.isNotEmpty(images)) {
            for (CreditAlbumVo image : images) {
                if (null == image.getId() || image.getId() == 0) {
                    VeVehicleAlbum veVehicleAlbum = new VeVehicleAlbum();
                    veVehicleAlbum.setVehicleId(Long.valueOf(veVehicleEvaluation.getId()));
                    veVehicleAlbum.setType(CreditAlbumType.IMAGE.type);
                    veVehicleAlbum.setUrl(image.getUrl());
                    veVehicleAlbumMapper.insert(veVehicleAlbum);
                }
            }
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult input(VehicleEvaluationInputBo vehicleEvaluationInputBo) {
        VeVehicleEvaluation veVehicleEvaluation = veVehicleEvaluationMapper
            .selectById(vehicleEvaluationInputBo.getVehicleEvaluationId());
        if (null == veVehicleEvaluation) {
            return AjaxResult.error("无该申请数据");
        }
        if (VehicleStatus.COMPLETE.status == veVehicleEvaluation.getStatus()) {
            return AjaxResult.error("已录入无法编辑");
        }
        BeanUtils.copyBeanProp(veVehicleEvaluation, vehicleEvaluationInputBo);

        if (OperatorStatus.SAVE.status == vehicleEvaluationInputBo.getOperatorStatus()) {
            veVehicleEvaluation.setStatus(VehicleStatus.SUBMIT.status);
        } else if (OperatorStatus.SUBMIT.status == vehicleEvaluationInputBo.getOperatorStatus()) {
            veVehicleEvaluation.setStatus(VehicleStatus.COMPLETE.status);
            veVehicleEvaluation.setSubmitAdminId(SecurityUtils.getLoginUserId());
            veVehicleEvaluation.setSubmitTime(LocalDateTime.now());
        } else if (OperatorStatus.REVOKE.status == vehicleEvaluationInputBo.getOperatorStatus()) {
            veVehicleEvaluation.setStatus(VehicleStatus.REVOKE.status);
        }
        veVehicleEvaluation.setInputTime(LocalDateTime.now());
        veVehicleEvaluation.setInputAdminId(SecurityUtils.getLoginUserId());
        veVehicleEvaluationMapper.updateById(veVehicleEvaluation);

        //添加图片信息
        List<CreditAlbumVo> images = vehicleEvaluationInputBo.getImages();
        if (StringUtils.isNotEmpty(images)) {
            for (CreditAlbumVo image : images) {
                if (null == image.getId() || image.getId() == 0) {
                    VeVehicleAlbum veVehicleAlbum = new VeVehicleAlbum();
                    veVehicleAlbum.setVehicleId(Long.valueOf(veVehicleEvaluation.getId()));
                    veVehicleAlbum.setType(CreditAlbumType.IMAGE.type);
                    veVehicleAlbum.setUrl(image.getDbUrl());
                    veVehicleAlbumMapper.insert(veVehicleAlbum);
                }
            }
        }
        return AjaxResult.success();
    }

    /**
     * 新增车辆评估
     *
     * @param veVehicleEvaluation 车辆评估
     * @return 结果
     */
    @Override
    public int insertVeVehicleEvaluation(VeVehicleEvaluation veVehicleEvaluation) {

        return veVehicleEvaluationMapper.insert(veVehicleEvaluation);
    }

    /**
     * 修改车辆评估
     *
     * @param veVehicleEvaluation 车辆评估
     * @return 结果
     */
    @Override
    public int updateVeVehicleEvaluation(VeVehicleEvaluation veVehicleEvaluation) {

        return veVehicleEvaluationMapper.updateById(veVehicleEvaluation);
    }

    /**
     * 删除车辆评估对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteVeVehicleEvaluationByIds(Collection<Integer> ids) {
        return veVehicleEvaluationMapper.deleteBatchIds(ids);
    }

    /**
     * 删除车辆评估信息
     *
     * @param id 车辆评估ID
     * @return 结果
     */
    @Override
    public int deleteVeVehicleEvaluationById(Integer id) {
        return veVehicleEvaluationMapper.deleteById(id);
    }
}
